package cn.edu.ctbu.jpatest.api;

import cn.edu.ctbu.jpatest.entity.Student;
import cn.edu.ctbu.jpatest.entity.Teacher;
import cn.edu.ctbu.jpatest.service.TeacherService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaopeng
 * @version 1.0
 */
@RestController
@RequestMapping("/api/teacher")
@RequiredArgsConstructor
public class TeacherApiController {

    private final TeacherService teacherService;

    /**
     * 获取所有教师
     * @return
     */
    @GetMapping("/list")
    public List<Teacher> getAllTeachers() {
        return teacherService.getAll();
    }

    /**
     * 根据id查询教师
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Teacher findById(@PathVariable Integer id){
        return teacherService.findById(id);
    }

    /**
     * 根据教师姓名模糊查询教师信息
     * @param name
     * @return
     */
    @GetMapping("/query-name")
    public List<Teacher> findByNameStartWith(String name) {
        return teacherService.findByNameStartWith(name);
    }

    /**
     * 添加教师
     * @param teacher
     * @return
     */
    @PostMapping("/add")
    public Teacher add(Teacher teacher){
        return teacherService.insert(teacher);
    }

    /**
     * 更新教师
     * @param teacher
     * @return
     */
    @PutMapping("/update")
    public Teacher update(Teacher teacher){
        return teacherService.update(teacher);
    }

    /**
     * 根据id删除教师
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public void deleteById(@PathVariable Integer id){
        teacherService.deleteById(id);
    }

}























